package com.atguigu.java.ai.langchain4j.config;

import com.atguigu.java.ai.langchain4j.store.MongoChatMemoryStore;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//聊天助手能够支持多用户并发对话，每个用户都有自己的对话历史记录，互不干扰
@Configuration
public class SeparateChatAssistantConfig {
    @Autowired
    private MongoChatMemoryStore MongoChatMemoryStore;

    @Bean
    public ChatMemoryProvider chatMemoryProvider(){
        return memoryId -> MessageWindowChatMemory
                .builder()
                .id(memoryId)
                .maxMessages(20)
//                .chatMemoryStore(new InMemoryChatMemoryStore())//ConcurrentHashMap形式，存储格式为 {memoryId, List<ChatMessage>}
                .chatMemoryStore(MongoChatMemoryStore)
                .build();
        /*
        等价于匿名实现类
        return new ChatMemoryProvider() {
            @Override
            public ChatMemory get(Object memoryId) {
                return MessageWindowChatMemory.builder()
                        .id(memoryId)
                        .maxMessages(20)
                        .build();
            }
        };
        */
    }
}
